package com.zscat.mallplus.controller.single;

import com.zscat.mallplus.entity.dto.ConsultDto;
import com.zscat.mallplus.entity.vo.oms.EvaluationCostmOrder;
import com.zscat.mallplus.entity.vo.oms.EvaluationOrder;
import com.zscat.mallplus.entity.vo.pms.PmsProductConsultInfoVo;
import com.zscat.mallplus.service.oms.service.IOmsEvaluateService;
import com.zscat.mallplus.service.oms.service.IOmsOrderService;
import com.zscat.mallplus.service.ums.service.IUmsMemberService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.vo.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZuRongTang
 * @Date 2020/3/4
 **/

@RestController
@Api("订单评价")
@RequestMapping("/api/evaluate")
public class OmsEvaluateController {

    @Resource
    private IUmsMemberService memberService;

    @Resource
    private IOmsEvaluateService omsEvaluateService;


    //评价已完成得订单
    @ApiOperation("获取已完成得订单评价列表")
    @RequestMapping(value = "/completedSuccesOrders", method = RequestMethod.GET)
    public Object completedSuccesOrders(String orderSn) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            commonResult.failed("请登录");
        }

        if (StringUtils.isEmpty(orderSn)) {
            commonResult.paramFailed();
        }

        return omsEvaluateService.completedSuccesOrders(memberId, orderSn);
    }

    @ApiOperation("获取我已完成得评价列表")
    @RequestMapping(value = "/getMyCompletedEvaluation", method = RequestMethod.GET)
    public Object getMyCompletedEvaluation(Integer pageIndex, Integer pageSize) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            commonResult.failed("请登录");
        }

        PageInfo<PmsProductConsultInfoVo> myCompletedEvaluation = omsEvaluateService.getMyCompletedEvaluation(memberId, pageIndex, pageSize);

        if (myCompletedEvaluation == null) {
            return commonResult.success(new ArrayList<>());
        }

        return commonResult.success(myCompletedEvaluation);
    }


    @ApiOperation("获取我未完成得评价列表")
    @RequestMapping(value = "/getUndoneEvaluation", method = RequestMethod.GET)
    public Object getUndoneEvaluation(Integer pageIndex, Integer pageSize) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            commonResult.failed("请登录");
        }

        PageInfo<EvaluationCostmOrder> undoneEvaluation = omsEvaluateService.getUndoneEvaluation(memberId, pageIndex, pageSize);

        if (undoneEvaluation == null) {
            return commonResult.success(new ArrayList<>());
        }

        return commonResult.success(undoneEvaluation);
    }


    //匿名
    @ApiOperation("评价设置匿名")
    @RequestMapping(value = "/setAnonymous", method = RequestMethod.GET)
    public Object setAnonymous(Long id) {
        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            commonResult.failed("请登录");
        }

        if (id == null) {
            commonResult.paramFailed();
        }

        return omsEvaluateService.setAnonymous(memberId, id);
    }

    //不可评论
    @ApiOperation("设置不可评论")
    @RequestMapping(value = "/setCannotComment", method = RequestMethod.GET)
    public Object setCannotComment(Long id) {

        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            commonResult.failed("请登录");
        }

        if (id == null) {
            commonResult.paramFailed();
        }

        return omsEvaluateService.setCannotComment(memberId, id);
    }

    //隐藏
    @ApiOperation("设置隐藏评论")
    @RequestMapping(value = "/setHideComment", method = RequestMethod.GET)
    public Object setHideComment(Long id) {

        CommonResult commonResult = new CommonResult();

        Long memberId = memberService.getNewCurrentMember().getId();

        if (memberId == null) {
            commonResult.failed("请登录");
        }

        if (id == null) {
            commonResult.paramFailed();
        }
        return omsEvaluateService.setHideComment(memberId, id);
    }


    @ApiOperation("评价商品")
    @RequestMapping(value = "/addGoodsConsult", method = RequestMethod.POST)
    public Object addGoodsConsult(@RequestBody ConsultDto consultDto) {
        CommonResult commonResult = new CommonResult();

        UmsMember newCurrentMember = memberService.getNewCurrentMember();

        Long memberId = newCurrentMember.getId();
        if (memberId == null) {
            return commonResult.failed("请登录");
        }

        if (consultDto == null) {
            return commonResult.paramFailed();
        }

        return omsEvaluateService.addGoodsConsult(memberId,newCurrentMember.getNickname(), consultDto);
    }

    //获取评论介绍
    @ApiOperation("获取评论介绍")
    @RequestMapping(value = "/getConsultIntroduction", method = RequestMethod.GET)
    public Object getConsultIntroduction() {
        String consultIntroduction = omsEvaluateService.getConsultIntroduction();
        return new CommonResult().success(consultIntroduction);
    }

}
